package com.huyn.baseframework.dynamicload.video;

import android.content.Context;
import android.view.ViewGroup;

import com.huyn.baseframework.dynamicload.video.model.GoodsItem;
import com.huyn.baseframework.model.ChannelDetail;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huyaonan on 16/3/23.
 */
public abstract class AbstractController<T> {

    public final String TAG = getClass().getSimpleName();

    protected IControllerCallback iControllerCallback;

    protected ViewGroup mCardRoot;

    protected Context context;
    protected ChannelDetail mChannel;

    public AbstractController(Context context, ViewGroup mRoot) {
        this.context = context;
        this.mCardRoot = mRoot;
    }

    public AbstractController(Context context, ViewGroup mRoot, ChannelDetail mChannel, IControllerCallback callback) {
        this.context = context;
        this.mChannel = mChannel;
        this.mCardRoot = mRoot;
        this.iControllerCallback = callback;
    }

    public void init(ChannelDetail mChannel, IControllerCallback callback) {
        this.mChannel = mChannel;
        this.iControllerCallback = callback;
    }

    protected List<T> mCardsList = new ArrayList<>();

    /**
     * 组装卡片顺序列表,根据当前播放位置来装入卡片,如果是刚开始,则selectedIndex为0
     */
    public abstract boolean setupCards(long selectedIndex, boolean freshData, boolean autoBegin);
    public void reverseFromFullScreen() {}
    public void start() {}
    public void pause() {}
    public void pauseWhenFreeze() {}
    public void seek(long target) {}

    protected T mCurrentData = null;

    /**
     * 根据播放位置加载卡片
     * @param current
     */
    public abstract boolean locateCardByTime(long current);

    public void clearCardsList() {
        synchronized (mCardsList) {
            mCardsList.clear();
        }
    }

    public abstract boolean manageCard(T data);

    public abstract void appendToScreen(T data);

    public void removeFromScreen() {
        mCardRoot.removeAllViews();
    }

    public void end() {
        mCurrentData = null;
        removeFromScreen();
        clearCardsList();
    }

    public void onDestroy() {
        iControllerCallback = null;
        clearCardsList();
    }

    public boolean onBack() {
        return false;
    }

    public interface IControllerCallback {
        public void onGoodsAdd(List<GoodsItem> data);
        public void managerTools();
        public void reserveFullScreen(boolean exitMultiVideo);
        public void toFullScreen(String title, boolean isVr);
        public void switchGoodsHotpotSize(boolean fullScreen);
        public void cancelGoods(String id);
        public void updateTitle(String title);
        public void setBg(String url);
        public void manageProgress(boolean speedUp, float ratio, boolean doSeek);
        public long getCurrentProgress();
        public long getDuration();
        public int[] getCartLocation();
        public void showTools(int time);
        public void cancelSeek();
        public void hideFavorCart();
        public void videoTipsIn(String tips);
        public void videoTipsOut();
        public void mainVideoChanged(int current);
        public void initToolBar(boolean onMajorVideo);
        public void endUpdateProgress();
        public void onStartTrackingTouch();
        public void pauseAllPlayers();
        public boolean isPlaying();
        public void updateProgressBySubVideo(long current);
        public void updateBg(String url);
    }
    
}
